package com.shandiangou.crm.controller;

import com.shandiangou.crm.model.vo.BizOrderRateVo;
import com.shandiangou.crm.model.vo.RateItemVo;
import com.shandiangou.crm.model.vo.RateVo;
import com.shandiangou.crm.util.CustomObjectMapper;
import com.shandiangou.crm.web.HttpResult;
import com.shandiangou.member.domain.UserDO;
import com.shandiangou.member.service.UserService;
import com.shandiangou.member.session.SdgSessionUtil;
import com.shandiangou.rate.domain.BizOrderRate;
import com.shandiangou.rate.domain.RateCategory;
import com.shandiangou.rate.domain.RateItem;
import com.shandiangou.rate.service.RateCategoryService;
import com.shandiangou.rate.service.RateService;
import com.shandiangou.trade.api.TradeService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.beans.PropertyDescriptor;
import java.util.*;

/**
 * Created by jacky on 16/1/20.
 * 会员相关业务的Controllerc层
 */

@Controller
@RequestMapping("/crm/member")
public class MemberController {

    private static final Logger logger = LoggerFactory.getLogger(MemberController.class);

    @Autowired
    private UserService userService;//用户中心服务

    @Autowired
    private RateService rateService;//评价业务

    @Autowired
    private RateCategoryService rateCategoryService;//评价类目业务

    @Autowired
    private TradeService tradeService;//交易业务

    /**
     * 获取用户信息
     *
     * @return
     */
    private UserDO getUser() {
        if (SdgSessionUtil.getUserMobile() != null) {
            String mobile = SdgSessionUtil.getUserMobile().toString();
            return userService.getUserByMobile(mobile);
        } else {
            return null;
        }
    }

    /**
     * 获得订单的评价
     * @param rateVo
     * @return
     */
    @RequestMapping(value = "/getBizOrderRatesByBizOrderId", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getBizOrderRatesByBizOrderId(@RequestBody RateVo rateVo) {
        try {
            UserDO oUserDO = getUser();
            if (oUserDO != null) {
                List<BizOrderRate> list = rateService.getBizOrderRatesByBizOrderId(rateVo.getBizOrderId());
                List<BizOrderRateVo> returnList = new ArrayList<>();
                for(BizOrderRate bizOrderRate : list){
                    BizOrderRateVo bizOrderRateVo = new BizOrderRateVo();
                    BeanUtils.copyProperties(bizOrderRate,bizOrderRateVo);
                    String pictures = bizOrderRate.getPictures();//上传的图片
                    List<RateItemVo> rateItemVoList = new ArrayList<>();
                    if(bizOrderRate.getRateItems() != null && !bizOrderRate.getRateItems().isEmpty()){
                        for(RateItem rateItem : bizOrderRate.getRateItems()){//获取评价类目的名称
                            RateItemVo rateItemVo = new RateItemVo();
                            BeanUtils.copyProperties(rateItem,rateItemVo);
                            RateCategory rateCategory = rateCategoryService.getCategoryById(rateItemVo.getCategoryId());
                            if(rateCategory != null){
                                rateItemVo.setCategoryName(rateCategory.getName());
                            }
                            rateItemVoList.add(rateItemVo);
                        }
                    }
                    bizOrderRateVo.setRateItemVoList(rateItemVoList);
                    bizOrderRateVo.setRateItems(null);//清空原来的数据，减少返回的数据量
                    if (StringUtils.isNotEmpty(pictures)) {//开始处理图片，多张图片以List返回
                        String[] picArray = StringUtils.split(pictures, ';');
                        bizOrderRateVo.setPicList(Arrays.asList(picArray));
                    }
                    returnList.add(bizOrderRateVo);
                }
                return HttpResult.Success(returnList);
            } else {
                return HttpResult.Failure(-98, "权限不足！");
            }
        } catch (Exception e) {
            logger.error("getBizOrderRatesByBizOrderId", e);
            return HttpResult.Failure(-99, "操作异常！");
        }
    }

    /**
     * 更新评价处理结果
     * @param rateVo
     * @return
     */
    @RequestMapping(value = "/updateRate", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult updateRate(@RequestBody RateVo rateVo) {
        try {
            UserDO oUserDO = getUser();
            if (oUserDO != null) {
                BizOrderRate bizOrderRate = new BizOrderRate();
                BeanUtils.copyProperties(rateVo, bizOrderRate);
                rateService.updateRateInfoByBizOrderId(bizOrderRate);
                return HttpResult.Success("成功！","成功！");
            } else {
                return HttpResult.Failure(-98, "权限不足！");
            }
        } catch (Exception e) {
            logger.error("updateRate", e);
            return HttpResult.Failure(-99, "操作异常！");
        }
    }

    /**
     * 搜索评价
     * @param rateVo
     * @return
     */
    @RequestMapping(value = "/searchRate", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult searchRate(@RequestBody RateVo rateVo) {
        try {
            UserDO oUserDO = getUser();
            if (oUserDO != null) {
                Map map = CustomObjectMapper.init().convertValue(rateVo, Map.class);
                List<BizOrderRate> list = rateService.getBizOrderRates(map);
                List<BizOrderRateVo> returnList = new ArrayList<>();
                for(BizOrderRate bizOrderRate : list){
                    BizOrderRateVo bizOrderRateVo = new BizOrderRateVo();
                    BeanUtils.copyProperties(bizOrderRate,bizOrderRateVo);
                    bizOrderRateVo.setBizOrderDO(tradeService.getOrderByBizOrderId(bizOrderRate.getBizOrderId()));
                    returnList.add(bizOrderRateVo);
                }
                return HttpResult.Success(returnList);
            } else {
                return HttpResult.Failure(-98, "权限不足！");
            }
        } catch (Exception e) {
            logger.error("searchRate", e);
            return HttpResult.Failure(-99, "操作异常！");
        }
    }
}
